package com.gxa.uhome.service.impl;

import com.gxa.uhome.exception.BusinessException;
import com.gxa.uhome.mapper.GoodsTypeMapper;
import com.gxa.uhome.pojo.GoodsType;
import com.gxa.uhome.service.GoodsTypeService;
import org.aspectj.weaver.ast.Var;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * User: Sanruy Mu
 * Date: 2021/06/07 14:07
 * Description:
 * Version: V1.0
 */
@Service("goodsTypeSerice")
public class GoodsTypeServiceImpl implements GoodsTypeService {
    @Resource
    GoodsTypeMapper goodsTypeMapper;

    @Override
    public Integer add(GoodsType goodsType) throws BusinessException {
        Integer add = goodsTypeMapper.add(goodsType);
        if (add != null) {
            return add;
        } else {
            throw new BusinessException("注册失败，请输入正确数据");
        }
    }

    @Override
    public Integer edit(GoodsType goodsType) throws BusinessException {
        Integer edit = goodsTypeMapper.edit(goodsType);
        if (edit != null) {
            return edit;
        } else {
            throw new BusinessException("编辑失败，请输入正确数据");
        }
    }

    @Override
    public Integer delete(Integer id) throws BusinessException {
        Integer delete = goodsTypeMapper.delete(id);
        if (delete != null) {
            return delete;
        } else {
            throw new BusinessException("删除失败，请选择数据");
        }
    }

    @Override
    public List<GoodsType> selectByLikeName(String name) throws BusinessException {
        name = "%" + name + "%";
        List<GoodsType> goodsTypes = goodsTypeMapper.selectByLikeName(name);
        if (goodsTypes != null) {
            return goodsTypes;
        } else {
            throw new BusinessException("查询失败，请输入正确字符");
        }
    }


    @Override
    public List<GoodsType> selectByPageParent(Integer page, Integer size) throws BusinessException {
        Integer page1 = (page - 1) * size;
        List<GoodsType> goodsTypes = goodsTypeMapper.selectByPageParent(page1, size);

        if (goodsTypes != null) {
            return goodsTypes;
        } else {
            throw new BusinessException("查询失败");
        }
    }

    @Override
    public List<GoodsType> selectByPageSon(Integer id) {
        List<GoodsType> goodsTypes = goodsTypeMapper.selectByPageSon(id);
        return goodsTypes;
    }

    @Override
    public List<GoodsType> selectAll() throws BusinessException {
        List<GoodsType> goodsTypeList = goodsTypeMapper.selectAll();
        if (goodsTypeList != null) {
            return goodsTypeList;
        }else{
            throw new BusinessException("查询失败");
        }
    }

    @Override
    public Integer deleteByPid(Integer id) {
        Integer integer = goodsTypeMapper.deleteByPid(id);
        return integer;
    }

    @Override
    public GoodsType selectById(Integer pid) throws BusinessException {
        GoodsType goodsType = goodsTypeMapper.selectById(pid);
        if(goodsType != null){
            return goodsType;
        }else{
            throw new BusinessException("发生错误，未查询到父类商品类");
        }
    }
}